<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

/**
 * 库存查询
 */
class Stockfilter extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'stock';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
    }

    /**
     * 列表
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'];
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }
            $fields = 'order_stock,p.name,p.id,bar_code,unit,marketable,order_stock,upper_store_count,price,lower_store_count,purchase_price,p.sort,p.create_date,c.name as cate_name,contacts,s.name as sup_name';
            $data = $model->alias('a')
                ->join('product p','a.product_id = p.id')
                ->join('product_category c','p.category_id = c.id')
                ->join('supplier s','p.supplier_id = s.id')
                ->field($fields)
                ->where($where)
                ->limit($start,$length)
                ->select();

            foreach($data as $k => $v)
            {
                $v['stock_fee'] = $v['marketable'] * $v['price'] /100; //总库存价
                $v['purchase_price'] = $v['purchase_price'] /100;  //售价
                $v['sort'] = $k + 1; //序号
            }
            $count = $model->alias('a')
                ->join('product p','a.product_id = p.id')
                ->join('product_category c','p.category_id = c.id')
                ->join('supplier s','p.supplier_id = s.id')
                ->where($where)
                ->count();

            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return self::show($this->indexView, [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $request = $this->request;
        $param = $request->param();
        $id = $param['id'];
        $this->assign('id',$id);

        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model('product_batch_stock');

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'];
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }

            $where['product_id'] = $param['id'];
            $data = $model
                ->where($where)
                ->limit($start,$length)
                ->select();

            foreach($data as $k => $v)
            {
                $v['sort'] = $k + 1; //序号
            }
            $count = count($data);
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return self::show('detail', [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }

    private function show($template, $data = []) {

        return $this->view->fetch($template, $data);
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if(isset($param['name']) && $param['name']){
            $where['p.name'] = ['like', '%'.$param['name'].'%'];
        }
        if(isset($param['bar_code']) && $param['bar_code']){
            $where['p.bar_code'] = ['like', '%'.$param['bar_code'].'%'];
        }
        if(isset($param['range_date']) && $param['range_date']){
            $rangeDate = explode(' ~ ', $param['range_date']);
            $startTime = $rangeDate[0];
            $endTime = $rangeDate[1];

            $where['p.create_date'] = [['egt', $startTime], ['elt', $endTime], 'and'];
        }

        return $where;
    }
}
